package com.nd.qa.shareplate.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nd.qa.shareplate.IDao.SharePageMapper;
import com.nd.qa.shareplate.common.CPResultCode;
import com.nd.qa.shareplate.model.SharePage;
import com.nd.qa.shareplate.service.PageElementService;
import com.nd.qa.shareplate.service.PageService;

@Service("com.nd.qa.shareplate.service.PageService")
public class PageServiceImpl implements PageService{

	@Autowired
	private SharePageMapper pageDao;
	@Autowired
	private PageElementService elementService;
	
	@Override
	public void insertPageInfo(SharePage page) throws Exception{
		pageDao.insertPageInfo(page);
	}

	@Override
	public Map<String, Object> updatePageInfo(SharePage page) throws Exception{
		return pageDao.updatePageInfo(page);
	}

	@Override
	public void deletePageInfo() throws Exception{
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<HashMap<String, Object>> queryPageList(Integer moduleId) throws Exception{
		
		return pageDao.queryPageList(moduleId);
	}

	@Override
	public HashMap<String, Object> queryPageInfo(SharePage page) throws Exception{
		HashMap<String, Object> pageMap = pageDao.queryPageInfo(page);
		return pageMap;
	}

	@Override
	public List<HashMap<String, Object>> queryChildPageList(SharePage page) throws Exception{
		List<HashMap<String, Object>> childPageList = pageDao.queryChildPageList(page);
		return childPageList;
	}

	@Override
	public HashMap<String, Object> deleteChildPages(SharePage page) throws Exception{
		pageDao.deleteChildPages(page);
		HashMap<String, Object> returnMap = new HashMap<String,Object>();
		returnMap.put("code", CPResultCode.CP0000);
		returnMap.put("message", "删除子页面成功");
		return returnMap;
	}

	@Override
	public HashMap<String, Object> modifyPage(SharePage page) throws Exception{
		pageDao.modifyPage(page);
		HashMap<String, Object> returnMap = new HashMap<String,Object>();
		returnMap.put("code", CPResultCode.CP0000);
		returnMap.put("message", "修改页面成功");
		return returnMap;
	}

	@Override
	public Integer queryDepthForId(int pageId) throws Exception {
		return pageDao.queryDepthForId(pageId);
	}

	@Override
	public Integer queryMaxDepth(int moduleId) throws Exception {
		return pageDao.queryMaxDepth(moduleId);
	}

	@Override
	public List<SharePage> queryPagesForDepth(int moduleId, int depth)
			throws Exception {
		
		return pageDao.queryPagesForDepth(moduleId, depth);
	}

	@Override
	public SharePage queryPageById(int pageId) throws Exception {
		return pageDao.queryPageById(pageId);
	}

	@Override
	public SharePage queryTemplatePage(SharePage page) throws Exception {
		return pageDao.queryTemplatePage(page);
	}

	@Override
	public List<SharePage> queryChildPages(SharePage page) throws Exception {
		return pageDao.queryChildPages(page);
	}

	@Override
	public Integer queryPageSeqByName(String seqName) throws Exception {
		return pageDao.queryPageSeqByName(seqName);
	}

	@Override
	public void updateTargetPageInfo(SharePage page) throws Exception {
		pageDao.updateTargetPageInfo(page);
	}

	@Override
	public SharePage queryPageByElementId(Integer elementId) throws Exception {
		return pageDao.queryPageByElementId(elementId);
	}
	
	@Override
	public Integer updatePage(SharePage page) throws Exception {
		Integer res =  pageDao.updatePage(page);
		return res;
	}

	@Transactional
	@Override
	public void handleDragPage(HashMap<String, Object> param) throws Exception {
		Integer nativeParentPageId = Integer.valueOf(String.valueOf(param.get("nativeParentPageId")));
		Integer pageId = Integer.valueOf(String.valueOf(param.get("pageId")));
		Integer parentPageId = Integer.valueOf(String.valueOf(param.get("parentPageId")));
		Integer moduleId = Integer.valueOf(String.valueOf(param.get("moduleId")));
		//1.查询被拖拽页面的信息 pageId ,获取相应的element_target_id和parent_page_id
		SharePage sharePage = pageDao.queryPageById(pageId);
		SharePage parentPage = pageDao.queryPageById(parentPageId);
		//2.修改被拖拽页面信息中的父页面Id，即把parent_page_id改为 前端传进的parentPageId,还要修改被拖拽页面的层级数
		SharePage pPage = new SharePage();
		pPage.setPageId(pageId);
		pPage.setModuleId(moduleId);
		pPage.setParentPageId(parentPageId);
		pPage.setPageDepth(parentPage.getPageDepth()+1);
		Integer res = pageDao.updatePage(pPage);
		if (res!=1) {
			 throw new RuntimeException("修改当前被拖拽页面的信息返回结果，updatePage_return is "+ res+",事务回滚");
		}
		updateDragedPageToChild(pageId,moduleId,parentPage.getPageDepth()+1);
		//3.通过前端传进来的nativeParentPageId，和element_target_id 来修改新增被拖拽页面时所生成的元素信息(把page_id改为parentPageId)
		HashMap<String, Object> eleParm = new HashMap<String,Object>();
		eleParm.put("elementId", sharePage.getElementTargetId());
		eleParm.put("pageId", nativeParentPageId);
		eleParm.put("modifyPageId", parentPageId);
		Integer result = elementService.updateElmentInfo(eleParm);
		if (result!=1) {
			 throw new RuntimeException("修改页面的元素信息返回结果，updateElmentInfo_return is "+ result+",事务回滚");
		}
	}
	/**
	 * 修改被拖拽页面下子页面的层级关系
	 * @param dragPageId
	 * @return
	 * @throws Exception 
	 */
	public void updateDragedPageToChild(Integer dragPageId,Integer moduleId,Integer pageDepth) throws Exception{
			SharePage pPage = new SharePage();
			pPage.setParentPageId(Integer.valueOf(dragPageId));
			pPage.setModuleId(moduleId);
			List<SharePage> listPages = pageDao.queryChildPageForId(pPage);
			for (int i = 0; i < listPages.size(); i++) {
				SharePage childPage = listPages.get(i);
				SharePage cPage = new SharePage();
				cPage.setPageId(childPage.getPageId());
				cPage.setPageDepth(pageDepth+1);
				Integer result = pageDao.updateChildPage(cPage);
				if (result!=1) {
					 throw new RuntimeException("修改页面的层级关系返回结果，updateChildPage_return is "+ result+",事务回滚");
				}
				updateDragedPageToChild(childPage.getPageId(),childPage.getModuleId(),pageDepth+1);
			}
	}
	@Override
	public List<SharePage> queryChildPageForId(SharePage page) throws Exception {
		return pageDao.queryChildPageForId(page);
	}

	@Override
	public void updateChildPage(SharePage page) throws Exception {
		pageDao.updateChildPage(page);
	}

	@Override
	public List<SharePage> queryPagesById(SharePage page) throws Exception {
		return pageDao.queryPagesById(page);
	}
}
